package taskTracker.ui.lite;

import java.util.Random;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;

import org.j4me.logging.Log;

import taskTracker.Interval;
import taskTracker.Task;
import taskTracker.util.Helper;

/**
 * A dialog that shows the current status of the program.
 * It shows whether the program is currently stopped or logging time.
 * When logging time it shows you where you are logging time project/task,
 * and for how long time has been logged.
 * You can also edit the comment, change the time out to show the this dialog again.
 * Finally, you can dismiss this window or start/stop time logging.
 * @author aseldawy
 *
 */
public class StatusAlert extends Form {

	/**A string that displays hadith of the day*/
	private final StringItem lblHadith = new StringItem("Hadith of the day", ""); 
	
	/**A combo box that allows you to choose another project/task*/
	final ChoiceGroup cmbCurrentTask = new ChoiceGroup("Task", ChoiceGroup.EXCLUSIVE) {
		protected String mapToString(Object dataPart) {
			Task task = (Task) dataPart;
			return task.getParent().getName()+"/"+task.getName();
		}
	};
	
	/**A text area that allows you to change current notes.*/
	final TextField txtComment = new TextField("Comment", "", 64, TextField.ANY);
	
	/**A combo box that allows you to choose how often this dialog appears*/
	private ChoiceGroup cmbAlertAfter = new ChoiceGroup("Alert me after", ChoiceGroup.POPUP, new Interval[] {
			Interval.minutes(2), Interval.minutes(5), Interval.minutes(10), Interval.minutes(15),
			Interval.minutes(30), Interval.hours(1), Interval.hours(2)
	}, null);
	
	/**Hides this dialog without any changes.*/
	final Command cmdHide = new Command("Hide", Command.BACK, 1);
	
	/**Starts logging time. Only visible when current status is stopped.*/
	final Command cmdStart = new Command("Start", Command.SCREEN, 2);
	
	/**Stops logging time. Only visible when current status is logging.*/
	final Command cmdStop = new Command("Stop", Command.SCREEN, 3);
	
	/**
	 * Creates a new StatusAlert dialog
	 * @param alertAfterModel
	 */
	public StatusAlert() {
		super("TaskTracker");
		// Append UI items
		this.append(lblHadith);
		this.append(cmbCurrentTask);
		this.append(cmbAlertAfter);
		this.append(txtComment);
		
		// Add commands
		this.addCommand(cmdHide);
		this.addCommand(cmdStart);
		this.addCommand(cmdStop);
	}
	
	/**
	 * Prepares dialog to show the given information.
	 * @param topTasks - Tasks to be shown to the user to select from. Must be shown in this order.
	 * @param notes - notes entered by the user.
	 * @param logging - whether the application is currently logging time or not.
	 * @param currentTask - The current task that user logs time to
	 * @param alertAfter - how often this dialog appears.
	 */
	public void prepare(Task[] topTasks, String notes, boolean logging, Task currentTask, long alertAfter) {
		Log.debug("loading hadith");
		loadHadith(Math.abs(new Random().nextInt()) % 87 + 1);
		Log.debug("hadith file read");
		MainScreen.display.callSerially(new SelectIndex(cmbAlertAfter, cmbAlertAfter.indexOf(new Interval(alertAfter))));
		cmbCurrentTask.setLabel(logging? "Logging time to" : "Not logging time");
		
		// TODO aseldawy enhancement: don't remove tasks if they're the same
		cmbCurrentTask.deleteAll();
		for (int i=0;i<topTasks.length;i++) {
			cmbCurrentTask.append(topTasks[i], null);
		}
		Log.debug("prepared cmbCurrentTask");
		int taskIndex = cmbCurrentTask.indexOf(currentTask);
		if (taskIndex != -1)
			MainScreen.display.callSerially(new SelectIndex(cmbCurrentTask, taskIndex));
		this.txtComment.setString(notes);
		Log.debug("set text comment");
		if (logging) {
			this.removeCommand(cmdStart);
			this.addCommand(cmdStop);
		} else {
			this.removeCommand(cmdStop);
			this.addCommand(cmdStart);
		}
	}

	/**
	 * Loads the hadith with the given id
	 * @param i
	 */
	private void loadHadith(int id) {
		Log.debug("Will load hadith #"+id);
		String raw_text = Helper.readTextFile("/ahadith/"+id, "utf-8");
		Log.debug("Hadith: <"+raw_text+">");
		String[] parts = Helper.split(raw_text, "\r\n");
		Log.debug("Hadith #"+id+" split into "+parts.length+" parts");
		if (parts[2].length() < 2)
			lblHadith.setText(parts[0]);
		else
			lblHadith.setText(parts[2]);
	}

	public long getInterval() {
		return ((Interval)cmbAlertAfter.getSelectedObject()).period;
	}

	public String getComment() {
		return txtComment.getString();
	}

	public Task getTask() {
		return (Task) cmbCurrentTask.getSelectedObject();
	}
}
